package com.venutech.gongan.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.venutech.gongan.dao.DevicesQueryAllTaskMapper;
import com.venutech.gongan.entity.OpWork;
import com.venutech.gongan.service.DevicesQueryAllTaskService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.venutech.gongan.service.QdsxjService;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * @Author liuzhengli
 * @Description //TODO 定时任务
 * @Date  2021/8/9 0009 10:49
 * @Param * @param null:
 * @return
 */
@Service
public class DevicesQueryAllTaskServiceImpl implements DevicesQueryAllTaskService {
    Logger logger = LoggerFactory.getLogger(DevicesQueryAllTaskServiceImpl.class);
    @Resource
    DevicesQueryAllTaskMapper devicesQueryAllTaskMapper;
    @Autowired
    QdsxjService qdsxjService;
    @Autowired
    DevicesQueryAllTaskService devicesQueryAllTaskService;

    /**
     * @author liuzhengli 
     * @Description //TODO 运维数据获取并添加到数据库里面
     * @Date 2021/9/15 15:00
     * @return 
     */
    @Override
    public void toJavaObjectDeviceSquery(JSONArray array) {
        try {
           for (int i=0;i<array.size();i++){
               JSONObject json = JSONObject.parseObject(array.get(i).toString());
               OpWork opWork =new OpWork();
               opWork.setRepaire_id(json.get("REPAIR_ID") != null?json.get("REPAIR_ID").toString():null);
               opWork.setDev_name(json.get("DEV_NAME") != null?json.get("DEV_NAME").toString():null);
               opWork.setDev_code(json.get("DEV_CODE") != null?json.get("DEV_CODE").toString():null);
               opWork.setFault_type(json.get("FAULT_TYPE") !=null?json.get("FAULT_TYPE").toString():null);
               opWork.setFault_remark(json.get("REPAIR_REMARK") != null?json.get("REPAIR_REMARK").toString():null);
               opWork.setRepair_status(json.get("REPAIR_STATUS") != null?json.get("REPAIR_STATUS").toString():null);
               opWork.setFault_time(json.get("FAULT_TIME") != null?json.get("FAULT_TIME").toString():null);
               opWork.setResponse_man(json.get("RESPONSE_MAN") != null?json.get("RESPONSE_MAN").toString():null);
               opWork.setResponse_time(json.get("RESPONSE_TIME") != null?json.get("RESPONSE_TIME").toString():null);
               opWork.setResponse_remark(json.get("RESPONSE_REMARK") != null?json.get("RESPONSE_REMARK").toString():null);
               opWork.setRepair_man(json.get("REPAIR_MAN") != null?json.get("REPAIR_MAN").toString():null);
               opWork.setRepair_time(json.get("REPAIR_TIME") != null?json.get("REPAIR_TIME").toString():null);
               opWork.setRepair_remark(json.get("FAULT_REMARK") != null?json.get("FAULT_REMARK").toString():null);
                String dev_code=opWork.getDev_code();
               if(Objects.isNull(devicesQueryAllTaskMapper.distinct_Dev_Code(dev_code)) && Objects.nonNull(qdsxjService.getTaskByDev_code(dev_code))){//查询数据库里面是否已经存在相同的数据
                  devicesQueryAllTaskService.InsertOperationsObject(opWork);
               }
           }
        }catch (Exception e){
            e.printStackTrace();
            logger.info("Exception:e={}",e.getMessage());
        }
    }

    /**
     * 通过反射向数据库表中添加运维数据
     * @param opWork
     * @return
     */
    @Override
    public int InsertOperationsObject(OpWork opWork) {
        return devicesQueryAllTaskMapper.getInsertOperationsObject(opWork);
    }
}
